import type {H3Event} from 'h3';
import defu from "defu";
import {HEADER_ACCEPT_LANGUAGE, HEADER_AUTHENTICATE} from "~~/server/resources/constants/constant";

export const useGetRequest =
    async (event: H3Event) => {
        const query = getQuery(event);
        const options: object = {
            method: "GET",
            headers: header(event, {'Context-Type': 'application/json'}),
            query: query,
        };
        const {baseUrl} = useRuntimeConfig().public;
        return await event.$fetch(baseUrl + event.context.Api, options);
    };
export const usePostRequest =
    async (event: H3Event) => {
        const body = await readBody(event);
        const query = getQuery(event);
        const options: object = {
            method: "POST",
            headers: header(event, {'Context-Type': 'application/json'}),
            body: body,
            query: query,
        };
        const {serverHostst} = useRuntimeConfig().public;
        return await event.$fetch(serverHostst + event.context.Api, options);
    };
export const usePutRequest =
    async (event: H3Event) => {
        const body = await readBody(event);
        const query = getQuery(event);
        const options: object = {
            method: "PUT",
            headers: header(event, {'Context-Type': 'application/json'}),
            body: body,
            query: query,
        };
        const {serverHostst} = useRuntimeConfig().public;
        return await event.$fetch(serverHostst + event.context.Api, options);
    };
export const useDeleteRequest =
    async (event: H3Event) => {
        const body = await readBody(event);
        const query = getQuery(event);
        const options: object = {
            method: "DELETE",
            header: header(event, {'Context-Type': 'application/json'}),
            body: body,
            query: query,
        };
        const {serverHostst} = useRuntimeConfig().public;
        return await event.$fetch(serverHostst + event.context.Api, options);
    };
export const useFormRequest =
    async (event: H3Event) => {
        const formData = await readBody(event);
        const query = getQuery(event);
        const options: object = {
            method: "FORM",
            params: formData,
            query: query,
            headers: header(event, {'Context-Type': 'application/x-www-form-urlencoded'}),
            watch: new Date().getTime() + ""
        };
        const {serverHostst} = useRuntimeConfig().public;
        return await event.$fetch(serverHostst + event.context.Api, options);
    };

const header = (event: H3Event, header: object) => {
    const clientHeaders = getHeaders(event);
    const authorization = clientHeaders[HEADER_AUTHENTICATE.toLowerCase()];
    const config = useRuntimeConfig()
    const cookieLanguage = getCookie(event, config.public.languageCookieKey.toLowerCase());
    const language = cookieLanguage || clientHeaders[HEADER_ACCEPT_LANGUAGE.toLowerCase()];
    return defu({'Accept': '*', 'Accept-Language': language, 'Authorization': authorization}, header);
}


